//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ty.pdm.system.workFlow.listener.common;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.baseline.service.TyplmBaseLineMemberService;
import com.hustcad.plm.pdm.baseline.service.TyplmBaseLineService;
import com.hustcad.plm.pdm.doc.service.TyplmDocumentService;
import com.hustcad.plm.pdm.doc.vo.DocDetails;
import com.hustcad.plm.pdm.ec.module.dto.ECObjData;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowChgFormView;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowObject;
import com.hustcad.plm.pdm.flowbusiness.model.entity.WorkFlowObjectPackage;
import com.hustcad.plm.pdm.partbom.dto.ChildPartsFiltersDTO;
import com.hustcad.plm.pdm.partbom.dto.PartBomTree;
import com.hustcad.plm.pdm.partbom.service.TyplmPartBomService;
import com.hustcad.plm.pdm.partbom.service.TyplmPartService;
import com.hustcad.plm.pdm.partbom.vo.PartBomTreeByFilterVO;
import com.hustcad.plm.pdm.partbom.vo.PartDetailsVO;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.annotion.CustomListenerField;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainerView;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import com.jsyxdl.k3cloud.utils.PlmPartOptionUtils;
import com.ty.basic.exception.message.SystemErrorCodeEnum;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.impl.el.FixedValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CustomListener(category = "评审对象检出校验，检出状态不允许启动流程"
        , description = "评审对象检出校验，检出状态不允许启动流程")
public class CheckOutVerificationListener implements ExecutionListener {
    private static final Logger log = LoggerFactory.getLogger(CheckOutVerificationListener.class);
    private static final long serialVersionUID = -9059663559349037414L;

    public void notify(DelegateExecution execution) {
        try {
            String workFlowContainerIdStr = execution.getProcessInstanceBusinessKey();
            BigInteger workFlowContainerId = new BigInteger(workFlowContainerIdStr);
            TyplmWorkFlowContainerService typlmWorkFlowContainerService = SpringUtil.getBean(
                    TyplmWorkFlowContainerService.class);

            if (typlmWorkFlowContainerService != null) {
                WorkFlowContainerView workFlowContainerView = typlmWorkFlowContainerService.getWorkFlowContainerDetailByPrimaryKey(
                        workFlowContainerId);
                if (workFlowContainerView instanceof WorkFlowObjectPackage) {
                    WorkFlowObjectPackage workFlowObjectPackage = (WorkFlowObjectPackage) workFlowContainerView;
                    if (workFlowObjectPackage.getFlowObjectList() != null && workFlowObjectPackage.getFlowObjectList()
                            .size() != 0) {

                        for (int i = 0; i < workFlowObjectPackage.getFlowObjectList().size(); i++) {
                            FlowObject flowObject = workFlowObjectPackage.getFlowObjectList().get(i);

                            extendedTask(flowObject);
                        }
                    } else {
                        throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException("流程评审对象为空");
                    }
                } else if (workFlowContainerView instanceof FlowChgFormView) {
                    FlowChgFormView flowChgFormView = (FlowChgFormView) workFlowContainerView;
                    if (flowChgFormView.getChgFormVO() != null && flowChgFormView.getChgFormVO()
                            .getEcObjDataVOS() != null) {

                        List<FlowObject> flowObjectList = new ArrayList<>();
                        for (int i = 0; i < flowChgFormView.getChgFormVO().getEcObjDataVOS().size(); i++) {
                            ECObjData ecObjData = flowChgFormView.getChgFormVO().getEcObjDataVOS().get(i);
                            BigInteger partOid = ecObjData.getRefObjectOid();
                            String oType = ecObjData.getRefObjectOtype();
                            FlowObject flowObject = new FlowObject();
                            flowObject.setRefObjectOid(partOid);
                            flowObject.setRefObjectOtype(oType);
                            flowObjectList.add(flowObject);
                        }
                        //排除重复的编码
                        Map<String, Object> cmd = new HashMap<>();
                        if (flowChgFormView.getChgFormVO().getEcObjDataVOS() != null) {
                            for (int i = 0; i < flowChgFormView.getChgFormVO().getEcObjDataVOS().size(); i++) {
                                ECObjData ecObjData = flowChgFormView.getChgFormVO().getEcObjDataVOS().get(i);
                                if (cmd.containsKey(ecObjData.getNumber())) {
                                    continue;
                                } else {
                                    cmd.put(ecObjData.getNumber(), "1");
                                }
                                extendedTaskEco(ecObjData);
                            }
                        }
                    }
                }
            }

        } catch (Exception var5) {
            var5.printStackTrace();
            throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(var5.getMessage());
        }
    }

    /**
     * 评审流程 BOM校验，有零部件检出不允许走流程 处理逻辑
     *
     * @param flowObject
     * @return
     */
    public void extendedTask(FlowObject flowObject) {

        TyplmPartService typlmPartService = SpringUtil.getBean(TyplmPartService.class);
        TyplmDocumentService typlmDocumentService = SpringUtil.getBean(TyplmDocumentService.class);

        BigInteger oid = flowObject.getRefObjectOid();
        String oType = flowObject.getRefObjectOtype();

        if ("ty.inteplm.part.CTyPart".equals(oType)) {
            PartDetailsVO partDetailsVO = typlmPartService.queryPartDetails(oid, oType);
            if ("c/i".equals(partDetailsVO.getCheckoutState()) == false) {
                throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                        "编码为【" + partDetailsVO.getObjectNumber() + "】对象被检出或锁定:");
            }
        } else if ("ty.inteplm.cad.CTyCADDoc".equals(oType)) {
            DocDetails docDetails = typlmDocumentService.queryDocDetails(oid, oType);
            if ("c/i".equals(docDetails.getCheckoutState()) == false) {
                throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                        "编码为【" + docDetails.getObjectNumber() + "】对象被检出或锁定:");
            }
        } else if ("ty.inteplm.doc.CTyDoc".equals(oType)) {
            DocDetails docDetails = typlmDocumentService.queryDocDetails(oid, oType);
            if ("c/i".equals(docDetails.getCheckoutState()) == false) {
                throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                        "编码为【" + docDetails.getObjectNumber() + "】对象被检出或锁定:");
            }
        }

    }

    /**
     * 变更流程 BOM校验，有零部件检出不允许走流程 处理逻辑
     *
     * @param ecObjData
     * @return
     */
    public void extendedTaskEco(ECObjData ecObjData) {

        TyplmPartService typlmPartService = (TyplmPartService) SpringUtil.getBean(TyplmPartService.class);
        TyplmDocumentService typlmDocumentService = SpringUtil.getBean(TyplmDocumentService.class);

        if ("ty.inteplm.part.CTyPart".equals(ecObjData.getOtype()) == false) {
            throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                    ecObjData.getObjectNumber() + " 不是零部件对象");
        }

        BigInteger oid = ecObjData.getRefObjectOid();
        String oType = ecObjData.getRefObjectOtype();
        if ("ty.inteplm.part.CTyPart".equals(oType)) {
            PartDetailsVO partDetailsVO = typlmPartService.queryPartDetails(oid, oType);
            if ("c/i".equals(partDetailsVO.getCheckoutState()) == false) {
                throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                        "编码为【" + partDetailsVO.getObjectNumber() + "】对象被检出或锁定:");
            }

        } else if ("ty.inteplm.cad.CTyCADDoc".equals(oType)) {
            DocDetails docDetails = typlmDocumentService.queryDocDetails(oid, oType);
            if ("c/i".equals(docDetails.getCheckoutState()) == false) {
                throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                        "编码为【" + docDetails.getObjectNumber() + "】对象被检出或锁定:");
            }
        } else if ("ty.inteplm.doc.CTyDoc".equals(oType)) {
            DocDetails docDetails = typlmDocumentService.queryDocDetails(oid, oType);
            if ("c/i".equals(docDetails.getCheckoutState()) == false) {
                throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                        "编码为【" + docDetails.getObjectNumber() + "】对象被检出或锁定:");
            }
        }

    }

}
